package com.xry.match.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xry.common.bean.Seat;
import com.xry.common.service.SeatService;
import com.xry.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/seat")
public class SeatController {
    @Autowired
    SeatService seatService;
    @GetMapping("{id}")
    public Result getSeatById(@PathVariable("id") Integer id) {
        try{
            Object obj=seatService.getSeatById(id);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @PostMapping("/addSeat")
    public Result addSeat(@RequestBody Seat seat) {
        try{
            seatService.addSeat(seat);
            seat = seatService.getSeatById(seat.getId());
            return Result.success("新增成功", seat);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @PutMapping("/updateSeatById")
    public Result updateSeatById(@RequestBody Seat seat) {
        try{
            boolean flag=seatService.updateSeatById(seat);
            if(flag){
                return Result.success("修改成功", seat);
            }else{
                return Result.fail(500,"修改失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @DeleteMapping("{id}")
    public Result deleteSeatById(@PathVariable("id") Integer id) {
        try{
            boolean flag=seatService.deleteSeatById(id);
            if(flag){
                return Result.success("删除成功");
            }else{
                return Result.fail(500,"删除失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getAll")
    public Result getPage() {
        try{
            Object obj=seatService.getAll();
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getPage")
    public Result getPage(@RequestParam(defaultValue = "1") Long current,
                          @RequestParam(defaultValue = "10") Long size,
                          Seat seat) {
        try{
            // 创建分页对象
            IPage<Seat> page = new Page<>(current, size);
            Object obj = seatService.getPage(seat, page);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }


}
